package com.thinkSoft.Location.Activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.thinkSoft.Location.R;
import com.thinkSoft.Location.Helpers.Debugger;
import com.thinkSoft.Location.Helpers.EulaCheck;
import com.thinkSoft.Location.Helpers.MotdCheck;
import com.thinkSoft.Location.Lib.DataFunc;
import com.thinkSoft.Location.Lib.WebData;
import com.thinkSoft.Location.Services.Background;

public class UIDashboard extends Activity {

	public final static String INTENT_ID="com.thinkSoft.Location.Motd";
	private Debugger debug = new Debugger(this);
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dashboard);
		checkLogin();
		if (WebData.isNetworkAvailable(UIDashboard.this)){
			new MotdCheck(this).execute();
		}
		new EulaCheck(this).show();
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		debug.postLog(Log.DEBUG, "Dashboard","Density: " + metrics.densityDpi);
		startService(new Intent(Background.class.getName()));
		Button btn_map = (Button) findViewById(R.id.btn_map);
		Button btn_settings = (Button) findViewById(R.id.btn_settings);
		Button btn_logout = (Button) findViewById(R.id.btn_logout);
		Button btn_help = (Button) findViewById(R.id.btn_help);

		btn_help.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view){
				Intent help = new Intent(getApplicationContext(), UIHelp.class);
				startActivity(help);
			}
		});

		btn_logout.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view){
				DataFunc userFunctions = new DataFunc();
				userFunctions.logoutUser(getApplicationContext());
				Intent login = new Intent(getApplicationContext(), UILogin.class);
				login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(login);
				finish();
			}
		});
		btn_map.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(), UIMap.class);
				startActivity(i);
			}
		});
		btn_settings.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(), UISettings.class);
				startActivity(i);
			}
		});

	}
	private void checkLogin() {
		DataFunc userDb = new DataFunc();
		if(!userDb.isUserLoggedIn(getApplicationContext())){
			Intent login = new Intent(getApplicationContext(), UILogin.class);
			login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(login);
			finish();            
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_dashboard, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, UISettings.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
